<?php
namespace Home\Controller;

use Think\Controller;

header("Content-Type: text/html;charset=utf-8");

class OrderController extends HomeController
{
    /*
     * 老师订单列表
     */
    public function teacherorder()
    {
        $id = session('id');
        if(I('get.type')=='all'){
            $where = ' r.teacher_id='.$id.' and order_form.status!=2';
            $tag=0;
        }elseif(I('get.type')=='in'){
            $where['order_form.status'] = 3;
            $where['r.teacher_id']=$id;
            $tag=1;
        }elseif(I('get.type')=='end'){
            $where['order_form.status'] = 5;
            $where['r.teacher_id']=$id;
            $tag=2;
        }else{
            $where['r.teacher_id']=$id;
            $tag=0;
        }
        
        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        $model = M("order_form"); 
        
        $limit = 8;
        $start = ($page - 1) * $limit;  
        
        $data = $model->join('LEFT JOIN requirement AS r ON r.id =  order_form.requirement_id ')->where($where)
            ->order('order_form.id DESC')->limit($start,$limit)
            ->field('order_form.status AS order_status, order_form.date,order_form.coursenum,order_form.order_sn, r.address,r.name,r.grade,r.lesson,r.remarks')->select();
        //echo $model->getLastSql();die();
        /*$total = $model->join('LEFT JOIN requirement AS r ON r.id =  order_form.requirement_id ')->where($where)->count();
        
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/teacherOrder') . '?page={page}' . $url;
        $render = $pagination->render();

        $this->assign('rander', $render);*/
        $this->assign('tag', $tag);
        $this->assign('lists', $data);
        $this->display('teacherorder');
    }

    /*
     * 家长订单中心
     */
    public function studentorder()
    {
        $this->judgeParent();
        $id = session('id');
        if(I('get.type')=='all'){
            $where['requirement.user_id']=$id;
            //$where = " requirement.user_id = $id";
        }elseif(I('get.type')=='inpay'){
            $where['rf.status'] = 2;
            $where['requirement.user_id']=$id;
        }elseif(I('get.type')=='handing'){
            $where['rf.status'] = 3;
            $where['requirement.user_id']=$id;
        }elseif(I('get.type')=='dealing'){
            $where = " requirement.user_id = $id AND (rf.status=1 OR rf.status=4)";
            //$where['rf.status'] = 1;
            //$where['rf.status'] = 4;
            //$where['requirement.user_id']=$id;
        }elseif(I('get.type')=='end'){
            $where['rf.status'] = 5;
            $where['requirement.user_id']=$id;
        }else{
            $where['requirement.user_id']=$id;
        }
        $url = "";

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }


//        switch (I('get.status')) {
//            case 0:
//                $where .= " AND requirement.status = 0";
//                $url = "&status=0";
//                break;
//            case 1:
//                $where .= " AND requirement.status IN (1,2)";
//                $url = "&status=1";
//                break;
//            case 2:
//                $where .= " AND requirement.status = 3";
//                $url = "&status=2";
//                break;
//        }
        $limit = 50;
        $start = ($page - 1) * $limit;

        $model = M("requirement");
        //获取课程数据
        $data = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')
                      ->join('LEFT JOIN user_teacher ut ON ut.user_id = requirement.teacher_id')
                      ->join('user u ON u.id = requirement.teacher_id')
                      ->where($where)->order('requirement.id desc')
                      ->limit($start,$limit)->field('requirement.*, ut.relname, ut.school, ut.sex,u.is_certified,rf.*')
                      ->select();
        foreach($data as $k=>$v){
            $map['grade'] = $v['grade'];
            $map['is_certified'] = $v['is_certified'];
            $result = M("course_price")->where($map)->find();
            $data[$k]['courseprice'] = $result['price'];
        }
        //$total = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')->join('LEFT JOIN  user_teacher ut ON rf.user_id = ut.user_id')->where($where)->count();

        /*$pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/parent') . '?page={page}' . $url;

        $render = $pagination->render();*/
        $this->assign('lists',$data);
        //$this->assign($status);
        $this->display('studentorder');
    }

    /**
     * student上课记录
     */
    public function studentrecord(){
        $orderlog = M("orderlog");
        $map['orderlog.order_sn'] = I('get.id');
        $map['orderlog.status'] = 1;
        $map['rf.user_id'] = session('id');
        $data = $orderlog->join('LEFT JOIN order_form rf ON rf.order_sn=orderlog.order_sn')
            ->where($map)->order('orderlog.id desc')
            ->field('orderlog.order_sn,orderlog.coursenum,orderlog.coursenum_c,orderlog.eventtime,orderlog.remark')
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['index'] = $k+1;
        }
        //echo $orderlog->getLastSql();die();
        $this->assign('lists',$data);
        $this->display('studyrecord');
    }

    /**
     * teacher上课记录
     */
    public function teacherrecord(){
        $orderlog = M("orderlog");
        $map['orderlog.order_sn'] = I('get.id');
        $map['orderlog.user_id'] = session("id");
        $map['orderlog.status'] = 1;
        $map['rf.teacher_id'] = session('id');
        $data = $orderlog->join('LEFT JOIN requirement rf ON rf.order_sn=orderlog.order_sn')
            ->where($map)->order('orderlog.id desc')
            ->field('orderlog.order_sn,orderlog.coursenum,orderlog.coursenum_c,orderlog.eventtime,orderlog.remark')
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['index'] = $k+1;
        }
        //echo $orderlog->getLastSql();die();
        $this->assign('lists',$data);
        $this->display('studyrecord');
    }
    /**
     * study
     */
    public function payrecord(){
        $order_form = M('order_form');
        $dmap['order_sn'] = I('get.id');
        $orderinfo = $order_form->where($dmap)->find();
        $orderlog = M("orderlog");
        $orderlog_comment = M("orderlog_comment");
        $map['orderlog.order_sn'] = array('like',I('get.id').'%');
        $map['orderlog.user_id'] = session('id');
        //$map['rf.user_id'] = session('id');
        $data = $orderlog//->join('LEFT JOIN orderlog_comment logcom logcom.comment_id = orderlog.comment_id')
            ->where($map)->order('orderlog.id desc')
            ->field('orderlog.id,orderlog.order_sn,orderlog.coursenum,orderlog.coursenum_c,orderlog.eventtime,orderlog.remark,orderlog.money,orderlog.status,orderlog.comment_id,orderlog.is_comment')
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['index'] = $k+1;
            $data[$k]['log_id'] = $v['id'];
            $data[$k]['isComment'] = $v['is_comment'];
            $data[$k]['user_id'] = $orderinfo['teacher_id'];
            if(isset($v['comment_id'])){
                unset($map);
                $map['comment_id'] = $v['comment_id'];
                $data[$k]['comment'] = $orderlog_comment->where($map)->find();
            }
        }
        //echo $orderlog->getLastSql();die();
        $this->assign('lists',$data);
        $this->assign('user_type',session('type'));
        //var_dump($data);
        //var_dump($data);
        $this->display('payrecord');
    }

    public function teacherwork(){
        $orderlog = M("orderlog");
        $home = new DefaultController();
        /*判断是否为第一个课时*/
        $map['order_sn'] = array('like',I('post.order_sn')."%");
        $map['user_id'] = session('id');
        $logcount = $orderlog->where($map)->count();
        if($logcount>=1){
            //1.减少课程
            $orderform = M("order_form");
            $ofmap['order_sn'] = I('post.order_sn');
            $reducenum = I('post.reducenum');
            $result = $orderform->where($ofmap)->find();
            $data['coursenum'] = (int)($result['coursenum'])-(int)($reducenum);
            if($data['coursenum']>0){
                $rs1 = $orderform->where($ofmap)->save($data);
                if(!$rs1){
                    $data['error'] = 1;
                    $data['msg'] = '扣除课程失败';
                    echo json_encode($data);die();
                }
            }elseif($data['coursenum']==0){
                $data['status'] = 5;
                $rs1 = $orderform->where($ofmap)->save($data);
                if(!$rs1){
                    $data['error'] = 1;
                    $data['msg'] = '扣除课程失败';
                    echo json_encode($data);die();
                }
            }elseif($data['coursenum']<0){
                $data['error'] = 1;
                $data['msg'] = '没有足够课时';
                echo json_encode($data);die();
            }
            //获取订单信息
            $remap['order_sn']=I('post.order_sn');
            $requirement = M("requirement");
            $req = $requirement->where($remap)->find();
            $pr['grade'] = $req['grade'];
            //判断剩余课时数，发短信提醒
            $user = M("user");
            $usermap['id']=$req['user_id'];
            $user = $user->where($usermap)->find();
            if($data['coursenum']<=2){
                $data['content'] = "【超老师中科大家教平台】尊敬的家长您好您的孩子有一门课程需要续费请登陆超老师网查看网址http://www.chaolaoshi.com";
                $home->sendSMSContent($user['phone'], $data['content']);
            }
            //发送备注给家长
            $content = "【超老师】尊敬的家长您好，我们的家教老师".$_POST['eventtime']."给您的孩子进行了".$_POST['reducenum']."小时的家教辅导，具体情况如下".$_POST['remark']."欢迎登陆超老师平台到我的课程里对本次课进行评价。如有任何疑问，请拨打我们的客服电话0551-63456053更多内容见超老师中科大家教平台chaolaoshi.com";
            $home->sendSMSContent($user['phone'], str_replace("\n","",$content));
            /****计算单价****/
            $CoursePriceHistory = M("course_price_history");
            $sphmap['is_over'] = 0;
            $sphmap['order_sn'] = I('post.order_sn');
            $cphresult = $CoursePriceHistory->where($sphmap)->find();
            $sphmap['id'] = $cphresult['id'];
            $sphdata['remain_course'] = $cphresult['remain_course'] - $reducenum;
            if($sphdata['remain_course']==0){
                $sphdata['is_over'] = 1;
            }
            $CoursePriceHistory->where($sphmap)->save($sphdata);
            /*$usemap['id']=session('id');
            $user = M("user");
            $us = $user->where($usemap)->find();
            $pr['grade'] = $req['grade'];
            $pr['is_certified'] = $us['is_certified'];
            $rs = M("course_price")->where($pr)->find();*/
            //2.1添加老师记录
            $orderlog = M("orderlog"); // 实例化User对象
            $data1['order_sn'] = I('post.order_sn');
            $data1['alipay_sn'] = '';
            $data1['status'] = 1;
            $data1['money'] = $cphresult['teacher_price']*$reducenum;
            $data1['coursenum'] = $data['coursenum'];
            $data1['coursenum_c'] = (int)($reducenum);
            $data1['eventtime'] = I('post.eventtime');
            $data1['remark'] = I('post.remark');
            $data1['addtime'] = date("Y-m-d H:i:s");
            $data1['user_id'] = session("id");
            $rs2 = $orderlog->add($data1);
            if(!$rs2){
                $data['error'] = 1;
                $data['msg'] = '添加老师记录失败';
                echo json_encode($data);die();
            }
            //2.2添加学生记录
            $orderlog = M("orderlog"); // 实例化User对象
            $data2['order_sn'] = I('post.order_sn');
            $data2['alipay_sn'] = '';
            $data2['status'] = 1;
            $data2['money'] = $cphresult['parent_price']*$reducenum;
            $data2['coursenum'] = $data['coursenum'];
            $data2['coursenum_c'] = (int)($reducenum);
            $data2['eventtime'] = I('post.eventtime');
            $data2['remark'] = I('post.remark');
            $data2['addtime'] = date("Y-m-d H:i:s");
            $data2['user_id'] = $result['user_id'];
            $rs3 = $orderlog->add($data2);
            if(!$rs3){
                $data['error'] = 1;
                $data['msg'] = '添加学生记录失败';
                echo json_encode($data);die();
            }
            //3.给老师加工资
            $techmap['user_id']=session('id');
            $rs3 = M('user_teacher')->where($techmap)->setInc('salary',$data1['money']);
            if(!$rs3){
                $data['error'] = 1;
                $data['msg'] = '给老师加工资失败';
                echo json_encode($data);die();
            }
            $data['error'] = 0;
            $data['msg'] = '成功登记课程';
            //4.返回结果
            echo json_encode($data);
        }else{
            //1.获取订单信息
            $remap['order_sn']=I('post.order_sn');
            $requirement = M("requirement");
            $req = $requirement->where($remap)->find();
            $pr['grade'] = $req['grade'];
            $orderform = M("order_form");
            //$map['order_sn'] = I('post.order_sn');
            $reducenum = I('post.reducenum');
            $result = $orderform->where($remap)->find();
            //2.发送备注给家长
            $user = M("user");
            $usermap['id']=$req['user_id'];
            $user = $user->where($usermap)->find();
            $content = "【超老师】唯一官方中科大家教平台。尊敬的家长您好！你在本平台的一个课程的剩余时间即将用完，为了保证课程正常进行，请你尽快登录超老师平台，在对应课程下进行续费。该课程号为".$result['order_sn']."如有任何疑问，请拨打客服电话0551-63456053超老师平台网址chaolaoshi.com";
            $home->sendSMSContent($user['phone'], str_replace("\n","",$content));
            //3.1添加老师记录
            $orderlog = M("orderlog"); // 实例化User对象
            $data1['order_sn'] = I('post.order_sn');
            $data1['alipay_sn'] = '';
            $data1['status'] = 1;
            $data1['money'] = 0;
            $data1['coursenum'] = $result['coursenum'];
            $data1['coursenum_c'] = 0;
            $data1['eventtime'] = I('post.eventtime');
            $data1['remark'] = "(第一课时为试讲)".I('post.remark');
            $data1['addtime'] = date("Y-m-d H:i:s");
            $data1['user_id'] = session("id");
            $rs2 = $orderlog->add($data1);
            if(!$rs2){
                $data['error'] = 1;
                $data['msg'] = '添加老师记录失败';
                echo json_encode($data);die();
            }
            //3.2添加学生记录
            $orderlog = M("orderlog"); // 实例化User对象
            $data2['order_sn'] = I('post.order_sn');
            $data2['alipay_sn'] = '';
            $data2['status'] = 1;
            $data2['money'] = 0;
            $data2['coursenum'] = $result['coursenum'];
            $data2['coursenum_c'] = 0;
            $data2['eventtime'] = I('post.eventtime');
            $data2['remark'] = "(第一课时为试讲)".I('post.remark');
            $data2['addtime'] = date("Y-m-d H:i:s");
            $data2['user_id'] = $result['user_id'];
            $rs3 = $orderlog->add($data2);
            if(!$rs3){
                $data['error'] = 1;
                $data['msg'] = '添加学生记录失败';
                echo json_encode($data);die();
            }
            $data['error'] = 0;
            $data['msg'] = '成功登记课程';
            //4.返回结果
            echo json_encode($data);
        }
    }

    public function complain(){
        $orderform = M("order_form");
        $map['order_sn'] = I('post.id');
        $map['user_id'] = session('id');
        $data['status'] = 4;
        $data['reback'] = I('post.reback');
        $rs = $orderform->where($map)->save($data);
        if($rs){
            $data['error']=0;
            $data['msg']  ='提交售后服务成功';
        }else{
            $data['error']=1;
            $data['msg']  ='提交售后服务失败';
        }
        echo json_encode($data);
    }

    public function addComment(){
        /*$_POST['log_id'] = 2;
        $_POST['total'] = 3;
        $_POST['accuracy'] = 4;
        $_POST['enrich'] = 5;
        $_POST['easy'] = 1;
        $_POST['experience'] = 2;
        $_POST['fast'] = 3;
        $_POST['remark'] = "评价";*/

        $orderlogComment = M("orderlog_comment");
        $orderlog = M("orderlog");
        //上课记录id
        $data['log_id'] = I('post.log_id');
        //student_id
        $data['student_id'] = session('id');
        //teacher_id
        $data['teacher_id'] = I('post.user_id');
        //总评价
        $data['total'] = I('post.total');
        //知识准确度
        $data['accuracy'] = I('post.accuracy');
        //内容充实度
        $data['enrich'] = I('post.enrich');
        //讲解透彻度
        $data['easy'] = I('post.easy');
        //讲解熟练度
        $data['experience'] = I('post.experience');
        //思维敏锐度
        $data['fast'] = I('post.fast');
        //评价意见
        $data['remark'] = I('post.remark');
        //添加时间
        $data['addtime'] = date("Y-m-d H:i:s");

        $id = $orderlogComment->add($data);

        if($id){
            unset($data);
            $map['id'] = I('post.log_id');

            $data['is_comment'] = 1;
            $data['comment_id'] = $id;

            $rs = $orderlog->where($map)->save($data);
            if($rs){
                $data['error']=0;
                $data['msg']  ='添加服务评价成功';
            }else{
                $data['error']=1;
                $data['msg']  ='修改上课记录失败';
            }
        }else{
            $data['error']= 1;
            $data['msg']  ='添加服务评价失败';
        }
        echo json_encode($data);
    }

    public function getComment(){
        $orderlogComment = M("orderlog_comment");

        $map['log_id'] = I('post.log_id');

        $comment = $orderlogComment->where($map)->find();

        echo json_encode($comment);
    }

}
